//
//  UIView+Configure.m
//  ZhouDao
//
//  Created by zengweihuan on 2018/5/24.
//  Copyright © 2018年 hyj. All rights reserved.
//

#import "UIView+Configure.h"
#import <YYCategories/YYCategories.h>
#import <YYText/YYText.h>
#import <CommonTools/CommonTools-Swift.h>

@implementation UIView (Configure)

+(instancetype)configureViewWithFrame:(CGRect)frame backgroundColor:(UIColor* _Nullable)backgroundColor inView:(UIView* _Nullable)inView
{
    UIView* view = [[self alloc]initWithFrame:frame];
    view.backgroundColor = backgroundColor;
    if (inView) {
        [inView addSubview:view];
    }
    return view;
}
+(instancetype)configureViewWithBackgroundColor:(UIColor* _Nullable)backgroundColor inView:(UIView* _Nullable)inView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    UIView* view = [[self alloc]init];
    if (inView) {
        [inView addSubview:view];
    }
    if (masConstraintBlock) {
        [view mas_makeConstraints:masConstraintBlock];
    }
    view.backgroundColor = backgroundColor;
    
    return view;
}
@end

@implementation UILabel (Configure)
+(instancetype)configureLabelWithFrame:(CGRect)frame  font:(CGFloat)font textColor:(UIColor* _Nullable)textColor inView:(UIView* _Nullable)aView
{
    UILabel* label = [[self alloc]initWithFrame:frame];
    label.font = [UIFont systemFontOfSize:font];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    if (aView) {
        [aView addSubview:label];
    }
    
    return label;
}

+(instancetype)configureLabelWithFont:(CGFloat)font textColor:(UIColor* _Nullable)textColor inView:(UIView* _Nullable)aView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    return [self configureLabelWithFontStyle:[UIFont systemFontOfSize:font] textColor:textColor inView:aView MASConstraintMaker:masConstraintBlock];
}

+(instancetype)configureLabelWithFontStyle:(UIFont * _Nullable)font textColor:(UIColor* _Nullable)textColor inView:(UIView* _Nullable)aView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    UILabel* label = [[self alloc]init];
    if (aView) {
        [aView addSubview:label];
    }
    if (masConstraintBlock) {
        [label mas_makeConstraints:masConstraintBlock];
    }
    
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    
    
    return label;
}
@end

@implementation UIImageView (Configure)
+(instancetype)configureIvWithOrigin:(CGPoint)origin image:(NSString* _Nullable)image inView:(UIView* _Nullable)inView
{
    UIImageView* iv = [[self alloc]initWithImage:[UIImage imageNamed:image] ];
    
    iv.contentMode = UIViewContentModeScaleAspectFill;
    iv.origin = origin;
    if (inView) {
        [inView addSubview:iv];
    }
    return iv;
}
+(instancetype)configureIvWithImage:(NSString* _Nullable)image inView:(UIView* _Nullable)inView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    UIImageView* iv = [[self alloc]initWithImage:[UIImage imageNamed:image]];
    if (inView) {
        [inView addSubview:iv];
    }
    if (masConstraintBlock) {
        [iv mas_makeConstraints:masConstraintBlock];
    }
    
    iv.contentMode = UIViewContentModeScaleAspectFill;
    return iv;
}
+(instancetype)configureIvWithFrame:(CGRect)frame inView:(UIView* _Nullable)aView
{
    UIImageView* iv = [[self alloc]initWithFrame:frame];
    if (aView) {
        [aView addSubview:iv];
    }
    return iv;
}
+(instancetype)configureIvWithView:(UIView* _Nullable)aView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    UIImageView* iv = [[UIImageView alloc]init];
    if (aView) {
        [aView addSubview:iv];
    }
    [iv mas_makeConstraints:masConstraintBlock];
    return iv;
}
@end

@implementation UIButton (Configure)

+(instancetype)configureBtnWithFrame:(CGRect)frame title:(NSString* _Nullable)title titleColor:(UIColor*)titleColor image:(NSString* _Nullable)image font:(CGFloat)font inView:(UIView* _Nullable)aView
{
    UIButton* btn = [[self alloc]initWithFrame:frame];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setImage:[UIImage imageWithBundleName:image] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    if (aView) {
        [aView addSubview:btn];
    }
    return btn;
}
+(instancetype)configureBtnWithTitle:(NSString* _Nullable)title titleColor:(UIColor* _Nullable)titleColor image:(NSString* _Nullable)image font:(CGFloat)font inView:(UIView* _Nullable)aView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock
{
    UIButton* btn = [[self alloc]init];
    if (aView) {
        [aView addSubview:btn];
    }
    if (masConstraintBlock) {
        [btn mas_makeConstraints:masConstraintBlock];
    }
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:titleColor forState:UIControlStateNormal];
    [btn setImage:[UIImage imageWithBundleName:image] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:font];
    
    return btn;
}

@end

@implementation UITextField (Configure)

+(instancetype)configureTextFieldWithFrame:(CGRect)frame font:(CGFloat)font textColor:(UIColor * _Nullable)textColor text:(NSString* _Nullable)text placeholder:(NSString* _Nullable)placeholder maxLength:(NSInteger)length inView:(UIView* _Nullable)aView  textChange:(void (^)(NSString* text))textChange
{
    UITextField* tf = [[self alloc]initWithFrame:frame];
    if (aView) {
        [aView addSubview:tf];
    }
    tf.textColor = textColor;
    tf.font = [UIFont systemFontOfSize:font];
    tf.text = text;
    tf.textAlignment = NSTextAlignmentRight;
    NSMutableAttributedString* att = [[NSMutableAttributedString alloc]initWithString:placeholder ? : @""];
    
    att.yy_font = [UIFont systemFontOfSize:font];
    att.yy_color = UIColorHex(0x999999);
    tf.attributedPlaceholder = att;
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [tf addBlockForControlEvents:UIControlEventEditingChanged block:^(UITextField*  _Nonnull textField) {
        
        NSUInteger maxLength = length;
 
        // text field 的内容
        NSString *contentText = textField.text;
        
        // 获取高亮内容的范围
        UITextRange *selectedRange = [textField markedTextRange];
        // 这行代码 可以认为是 获取高亮内容的长度
        NSInteger markedTextLength = [textField offsetFromPosition:selectedRange.start toPosition:selectedRange.end];
        // 没有高亮内容时,对已输入的文字进行操作
        if (markedTextLength == 0) {
            // 如果 text field 的内容长度大于我们限制的内容长度
            if (contentText.length > maxLength) {
                // 截取从前面开始maxLength长度的字符串
                //            textField.text = [contentText substringToIndex:maxLength];
                // 此方法用于在字符串的一个range范围内，返回此range范围内完整的字符串的range
                NSRange rangeRange = [contentText rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [contentText substringWithRange:rangeRange];
            }
            
            if (textChange) {
                textChange(textField.text);
            }
        }
    }];
    return tf;
}

+(instancetype)configureTextFieldWithFont:(CGFloat)font textColor:(UIColor * _Nullable)textColor text:(NSString* _Nullable)text placeholder:(NSString* _Nullable)placeholder maxLength:(NSInteger)length inView:(UIView* _Nullable)aView MASConstraintMaker:(MASConstraintBlock)masConstraintBlock  textChange:(void (^)(NSString* text))textChange
{
    UITextField* tf = [[self alloc]init];
    if (aView) {
        [aView addSubview:tf];
    }
    if (masConstraintBlock) {
        [tf mas_makeConstraints:masConstraintBlock];
    }
    tf.textColor = textColor;
    tf.font = [UIFont systemFontOfSize:font];
    tf.text = text;
    tf.textAlignment = NSTextAlignmentRight;
    NSMutableAttributedString* att = [[NSMutableAttributedString alloc]initWithString:placeholder ? : @""];
    att.yy_font = [UIFont systemFontOfSize:font];
    att.yy_color = UIColorHex(0xdddde1);
    tf.attributedPlaceholder = att;
    
    [tf addBlockForControlEvents:UIControlEventEditingChanged block:^(UITextField*  _Nonnull sender) {
        NSString *lang = [sender.textInputMode primaryLanguage];
        BOOL shouldChangeText = YES;
        if([lang isEqualToString:@"zh-Hans"]){ //简体中文输入，包括简体拼音，健体五笔，简体手写
            UITextRange *selectedRange = [sender markedTextRange];
            UITextPosition *position = [sender positionFromPosition:selectedRange.start offset:0];
            shouldChangeText = !position;
        }
        if (shouldChangeText)
        {
            sender.text = [sender.text substringToIndex:MIN(sender.text.length, length)];
            if (textChange) {
                textChange(sender.text);
            }
        }
    }];
    return tf;
}
@end
